//
//  FFRecvPayCardCell.m
//  LLWallet
//
//  Created by tt on 2020/12/16.
//

#import "FFRecvPayCardCell.h"
#import "LBXScanNative.h"
@implementation FFRecvPayCardModel

- (Class)cell {
    return FFRecvPayCardCell.class;
}

@end

@interface FFRecvPayCardCell ()

@property(nonatomic,strong) FFRecvPayCardModel *model;

@property(nonatomic,strong) UIView *boxView;

@property(nonatomic,strong) UIButton *moreBtn;

@property(nonatomic,strong) UIImageView *recvPayIcon;
@property(nonatomic,strong) UILabel *titleLbl;

@property(nonatomic,strong) UIView *bodyView;

@property(nonatomic,strong) UIImageView *qrcodeView;
@property(nonatomic,strong) UIImageView *barcodeView;

@property(nonatomic,strong) UIView *footerLineView;

@property(nonatomic,strong) UIView *footerView;

// 零钱
@property(nonatomic,strong) UIImageView *smallchangeIcon;
@property(nonatomic,strong) UILabel *smallchangeTitleLbl;
@property(nonatomic,strong) UILabel *smallchangeValueLbl;
@end

#define headerHeight 64.0f
#define barcodeHeight 140.0f

@implementation FFRecvPayCardCell

+ (CGSize)sizeForModel:(WKFormItemModel *)model {
    return CGSizeMake(WKScreenWidth, 500.0f);
}

- (void)setupUI {
    [super setupUI];
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.boxView];
    
    [self.boxView addSubview:self.moreBtn];
    
    [self.boxView addSubview:self.recvPayIcon];
    [self.boxView addSubview:self.titleLbl];
    
    [self.boxView addSubview:self.bodyView];
    
    [self.bodyView addSubview:self.qrcodeView];
    [self.bodyView addSubview:self.barcodeView];
    
    [self.bodyView addSubview:self.footerLineView];
    [self.bodyView addSubview:self.footerView];
    
    [self.footerView addSubview:self.smallchangeIcon];
    [self.footerView addSubview:self.smallchangeTitleLbl];
    [self.footerView addSubview:self.smallchangeValueLbl];
    
    
}

- (void)refresh:(FFRecvPayCardModel *)model {
    [super refresh:model];
    self.model = model;
    
    self.qrcodeView.image =  [LBXScanNative createQRWithString:model.qrcode?:@"" QRSize:CGSizeMake(self.qrcodeView.lim_size.width*2, self.qrcodeView.lim_size.height*2)];
    
    self.barcodeView.image = [LBXScanNative createBarCodeWithString:model.qrcode?:@"" QRSize:CGSizeMake((WKScreenWidth - 10.0f)*2, barcodeHeight*2)];
    
    self.smallchangeValueLbl.text = [NSString stringWithFormat:LLang(@"可用余额 ¥%@"),model.walletAmountFormat];
    [self.smallchangeValueLbl sizeToFit];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.boxView.lim_left = 10.0f;
    self.boxView.lim_height = self.contentView.lim_height;
    self.boxView.lim_width = self.contentView.lim_width - self.boxView.lim_left*2;
    self.boxView.lim_centerX_parent = self.contentView;
    
    self.moreBtn.lim_left = self.boxView.lim_width - self.moreBtn.lim_width - 15.0f;
    self.moreBtn.lim_top = headerHeight/2.0f - self.moreBtn.lim_height/2.0f;
    
    self.recvPayIcon.lim_left = 15.0f;
    self.recvPayIcon.lim_top = headerHeight/2.0f - self.recvPayIcon.lim_height/2.0f;
    
    self.titleLbl.lim_top =  headerHeight/2.0f - self.titleLbl.lim_height/2.0f;
    self.titleLbl.lim_left = self.recvPayIcon.lim_right + 10.0f;
    
    self.bodyView.lim_top = headerHeight;
    self.bodyView.lim_height = self.boxView.lim_height - headerHeight;
    self.bodyView.lim_width = self.boxView.lim_width;
    
    self.barcodeView.lim_top = 15.0f;
    self.barcodeView.lim_left = 10.0f;
    self.barcodeView.lim_width = self.boxView.lim_width - self.barcodeView.lim_left*2;
    self.barcodeView.lim_height = barcodeHeight;
    self.barcodeView.lim_centerX_parent = self.bodyView;
    
    self.qrcodeView.lim_top = self.barcodeView.lim_bottom + 10.0f;
    self.qrcodeView.lim_centerX_parent = self.bodyView;
    
    self.footerView.lim_height = headerHeight;
    self.footerView.lim_width = self.bodyView.lim_width;
    self.footerView.lim_top = self.bodyView.lim_height - self.footerView.lim_height;
    
    self.footerLineView.lim_left = 20.0f;
    self.footerLineView.lim_width = self.bodyView.lim_width - 20.0f*2;
    self.footerLineView.lim_top = self.footerView.lim_top;
    
    self.smallchangeIcon.lim_left = self.footerLineView.lim_left;
    self.smallchangeIcon.lim_top = 4.0f;
    
    CGFloat smallchangeValueTopSpace = 2.0f;
    CGFloat smallchangeHeight = self.smallchangeTitleLbl.lim_height + smallchangeValueTopSpace + self.smallchangeValueLbl.lim_height;
    self.smallchangeTitleLbl.lim_top = self.footerView.lim_height/2.0f - smallchangeHeight/2.0f;
    self.smallchangeTitleLbl.lim_left = self.smallchangeIcon.lim_right + 10.0f;
    
    self.smallchangeIcon.lim_top = self.smallchangeTitleLbl.lim_top;
    
    self.smallchangeValueLbl.lim_top = self.smallchangeTitleLbl.lim_bottom+smallchangeValueTopSpace;
    self.smallchangeValueLbl.lim_left = self.smallchangeTitleLbl.lim_left;
    
}

- (UIView *)boxView {
    if(!_boxView) {
        _boxView = [[UIView alloc] init];
        _boxView.backgroundColor = [UIColor colorWithRed:249.0f/255.0f green:249.0f/255.0f blue:249.0f/255.0f alpha:1.0f];
        _boxView.layer.masksToBounds = YES;
        _boxView.layer.cornerRadius = 8.0f;
    }
    return _boxView;
}

- (UIImageView *)recvPayIcon {
    if(!_recvPayIcon) {
        _recvPayIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 24.0f, 24.0f)];
        _recvPayIcon.image = [self imageName:@"recvpay_icon"];
    }
    return _recvPayIcon;
}

- (UILabel *)titleLbl {
    if(!_titleLbl) {
        _titleLbl = [[UILabel alloc] init];
        _titleLbl.textColor = [WKApp shared].config.themeColor;
        _titleLbl.font = [[WKApp shared].config appFontOfSize:17.0f];
        _titleLbl.text = LLang(@"向商家付款");
        [_titleLbl sizeToFit];
    }
    return _titleLbl;
}

- (UIView *)bodyView {
    if(!_bodyView) {
        _bodyView = [[UIView alloc] init];
        _bodyView.backgroundColor = [UIColor whiteColor];
    }
    return _bodyView;
}

- (UIImageView *)qrcodeView {
    if(!_qrcodeView) {
        _qrcodeView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 180.0f, 180.0f)];
    }
    return _qrcodeView;
}

- (UIImageView *)barcodeView {
    if(!_barcodeView) {
        _barcodeView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
        _barcodeView.backgroundColor = [UIColor redColor];
    }
    return _barcodeView;
}

- (UIView *)footerView {
    if(!_footerView) {
        _footerView = [[UIView alloc] init];
    }
    return _footerView;
}

- (UIView *)footerLineView {
    if(!_footerLineView) {
        _footerLineView = [[UIView alloc] init];
        _footerLineView.backgroundColor = [UIColor colorWithRed:249.0f/255.0f green:249.0f/255.0f blue:249.0f/255.0f alpha:1.0f];
        _footerLineView.lim_height = 1.0f;
    }
    return _footerLineView;
}

- (UIImageView *)smallchangeIcon {
    if(!_smallchangeIcon) {
        _smallchangeIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 24.0f, 24.0f)];
        _smallchangeIcon.image = [self imageName:@"smallchange"];
    }
    return _smallchangeIcon;
}

- (UILabel *)smallchangeTitleLbl {
    if(!_smallchangeTitleLbl) {
        _smallchangeTitleLbl = [[UILabel alloc] init];
        _smallchangeTitleLbl.text = LLang(@"零钱");
        _smallchangeTitleLbl.font = [[WKApp shared].config appFontOfSize:16.0f];
        _smallchangeTitleLbl.textColor = [UIColor blackColor];
        [_smallchangeTitleLbl sizeToFit];
    }
    return _smallchangeTitleLbl;
}

- (UILabel *)smallchangeValueLbl {
    if(!_smallchangeValueLbl) {
        _smallchangeValueLbl = [[UILabel alloc] init];
        _smallchangeValueLbl.font = [[WKApp shared].config appFontOfSize:15.0f];
        _smallchangeValueLbl.textColor = [WKApp shared].config.tipColor;
    }
    return _smallchangeValueLbl;
}

- (UIButton *)moreBtn {
    if(!_moreBtn) {
        _moreBtn = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
        [_moreBtn setImage:[self imageName:@"more"] forState:UIControlStateNormal];
        [_moreBtn addTarget:self action:@selector(morePressed) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

-(void) morePressed {
    if(self.model.onMore) {
        self.model.onMore();
    }
}

-(UIImage*) imageName:(NSString*)name {
    return [[WKApp shared] loadImage:name moduleID:@"LLWallet"];
}

@end
